<?php
/**
 * Created by PhpStorm.
 * User: lg
 * Date: 2017/12/6
 * Time: 15:58
 */

namespace app\bangding\controller;

use think\captcha\Captcha;
use think\Controller;
use think\exception\HttpResponseException;
use think\Response;
use EasyWeChat\Foundation\Application;

class PublicController extends Controller
{
    /**
     * ajax 返回方法
     * @param int $code
     * @param string $msg
     * @param string $url
     * @param array $data
     */
    public function _ajax_return($code = 0, $msg = "", $url = '', $data = [])
    {
        return $this->_return(['code' => $code, 'msg' => $msg, 'jumpUrl' => $url, 'data' => $data]);
    }

    /**
     * 返回
     */
    public function _return($response = [], $header = [])
    {

        $response = Response::create($response, 'json')->header($header);
        throw new HttpResponseException($response);
    }

    /**
     * 实例化easywechat
     */
    public function easywechat()
    {
        return new Application(config('wx_config'));
    }

    public function oauth()
    {
        return $this->easywechat()->oauth;
    }

    public function paymnet()
    {
        return $this->easywechat()->payment;
    }

    public function js()
    {
        return $this->easywechat()->js;
    }

    /**
     * 向页面中传入微信jssdk
     * @param $apiList
     * @param bool $debug
     */
    public function jssdk($apiList, $debug = false)
    {
        $jssdk = $this->js()->config($apiList, $debug);
        $this->assign('jssdk', $jssdk);
    }
}